﻿Shader "Custom/AlphaTest"
{
    Properties
    {
        _Color("Color Tint",Color) = (1,1,1,1)
        _MainTex ("Texture", 2D) = "white" {}
        _Cutoff("Alpha Cutoff",Range(0,1)) = 0.5
        _OutlineWidth ("Outline Width", float) = 1
        _OutlineColor ("Outline Color", Color) = (0,0,0,1)
    }
    SubShader
    {
        Tags { "RenderType"="TransparentCutout" "Queue"="AlphaTest" "IgnoreProjector"="True" "LightMode"="ForwardBase"}
        LOD 100
        Blend SrcAlpha OneMinusSrcAlpha

        

        // Pass
        // {
        //     ZWrite On
        //     ColorMask 0  //用于设置颜色通道的写掩码,0表示不写入任何通道
        // }
 
        Pass
        {
            Tags { "RenderType"="TransparentCutout" "Queue"="AlphaTest" }
            Blend SrcAlpha OneMinusSrcAlpha
            Cull Front
            ZWrite On

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
             
            #include "UnityCG.cginc"
            #include "Lighting.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float3 normal:NORMAL;
                float4 texcoord : TEXCOORD0;
            };
 
            struct v2f
            {
                float4 pos : SV_POSITION;
                float3 worldNormal:TEXCOORD0;
                float3 worldPos:TEXCOORD1;
                float2 uv : TEXCOORD2;
            };
 
            fixed4 _Color;
            fixed _Cutoff;
            sampler2D _MainTex;
            float4 _MainTex_ST;
             
            v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
                return o;
            }
             
            fixed4 frag (v2f i) : SV_Target
            {
                fixed3 worldNormal = normalize(i.worldNormal);
                fixed3 worldLightDir = UnityWorldSpaceLightDir(i.worldPos);
 
                fixed4 albedo = tex2D(_MainTex,i.uv)* _Color;
 
                //Alpha Test
                // clip(albedo.a - _Cutoff);
                //equal to
                //if(texColor.a - _Cutoff < 0) discard;
                
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo.rgb;
                fixed3 diffuse = _LightColor0.rgb * albedo.rgb * max(0,dot(worldNormal,worldLightDir));
                return fixed4(diffuse + ambient,albedo.a);
            }
            ENDCG
        }

        Pass
        {
            Tags { "RenderType"="TransparentCutout" "Queue"="AlphaTest" }
            Blend SrcAlpha OneMinusSrcAlpha
            Cull Back
            ZWrite On

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
             
            #include "UnityCG.cginc"
            #include "Lighting.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float3 normal:NORMAL;
                float4 texcoord : TEXCOORD0;
            };
 
            struct v2f
            {
                float4 pos : SV_POSITION;
                float3 worldNormal:TEXCOORD0;
                float3 worldPos:TEXCOORD1;
                float2 uv : TEXCOORD2;
            };
 
            fixed4 _Color;
            fixed _Cutoff;
            sampler2D _MainTex;
            float4 _MainTex_ST;
             
            v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
                return o;
            }
             
            fixed4 frag (v2f i) : SV_Target
            {
                fixed3 worldNormal = normalize(i.worldNormal);
                fixed3 worldLightDir = UnityWorldSpaceLightDir(i.worldPos);
 
                fixed4 albedo = tex2D(_MainTex,i.uv)* _Color;
 
                //Alpha Test
                // clip(albedo.a - _Cutoff);
                //equal to
                //if(texColor.a - _Cutoff < 0) discard;
                
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo.rgb;
                fixed3 diffuse = _LightColor0.rgb * albedo.rgb * max(0,dot(worldNormal,worldLightDir));
                return fixed4(diffuse + ambient,albedo.a);
            }
            ENDCG
        }

        // Pass
        // {
        //     ZWrite On
        //     ColorMask 0
        // }

        Pass
        {
            Tags { "RenderType"="TransparentCutout" "Queue"="AlphaTest" }
            Blend SrcAlpha OneMinusSrcAlpha
            Cull Front

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float4 tangent : TANGENT;
				float4 uv2     : TEXCOORD1;
                float4 uv3     : TEXCOORD2;
                float3 normal  : NORMAL;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float3 normal : TEXCOORD0;
                float3 avgNormal : TEXCOORD1;
                float3 normalVS : TEXCOORD2;
            };

            float _OutlineWidth;
            fixed4 _OutlineColor;

            v2f vert (appdata v)
            {
                v2f o;
                float4 pos = UnityObjectToClipPos(v.vertex);

                float3 normal = normalize(v.normal);
                float3 tangent = normalize(v.tangent.xyz);
                float3 biNormal = normalize(cross(normal, tangent)*v.tangent.w);
                float3x3 TBN = float3x3(tangent, biNormal, normal);
                float3 avgNormal = normalize(mul(transpose(TBN), v.uv2.xyz));

                float3 normalVS = mul((float3x3)UNITY_MATRIX_IT_MV, avgNormal);//v.uv3.xyz
                float3 ndcNormal = normalize(TransformViewToProjection(normalVS.xyz))*pos.w;
                float4 nearUpperRight = mul(unity_CameraInvProjection, float4(1,1,UNITY_NEAR_CLIP_VALUE, _ProjectionParams.y));
                float aspect = abs(nearUpperRight.y/nearUpperRight.x);
                ndcNormal.x *= aspect;
                pos.xy += 0.01 * _OutlineWidth * ndcNormal.xy;
                o.vertex = pos;
                o.normal = UnityObjectToWorldNormal(v.normal);
                o.avgNormal = mul((float3x3)UNITY_MATRIX_IT_MV, avgNormal);
                o.normalVS = normalVS;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // float3 color = (i.avgNormal - i.normalVS)*10;
                // return fixed4(color,1);
                return _OutlineColor;
            }
            ENDCG
        }
    }
}